<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script src="${ctx}/static/selectWin/postSelector.js?version=${applicationScope.jsVersion }" type="text/javascript"></script>
<title>人员管理</title>

<script type="text/javascript">
	function datagridField() {
		$grid.datagrid('enableFilter', [ {
			field : 'isPostLeader',
			type : 'combobox',
			javaType : 'I',
			options : {
				panelHeight : 'anto',
				data : [ {
					value : '',
					text : '全部'
				}, {
					value : '1',
					text : '是'
				}, {
					value : '2',
					text : '否'
				} ],
				onChange : function(value) {
					if (value == '') {
						$("#grid").datagrid('removeFilterRule', 'enable');
					} else {
						$("#grid").datagrid('addFilterRule', {
							field : 'isPostLeader',
							op : 'EQ',
							value : value
						});
					}
					$("#grid").datagrid('doFilter');
				}
			}
		}, {
			field : 'enable',
			type : 'combobox',
			javaType : 'I',
			options : {
				panelHeight : 'anto',
				data : [ {
					value : '',
					text : '全部'
				}, {
					value : '1',
					text : '是'
				}, {
					value : '2',
					text : '否'
				} ],
				onChange : function(value) {
					if (value == '') {
						$("#grid").datagrid('removeFilterRule', 'enable');
					} else {
						$("#grid").datagrid('addFilterRule', {
							field : 'enable',
							op : 'EQ',
							value : value
						});
					}
					$("#grid").datagrid('doFilter');
				}
			}
		}, {
			field : 'sex',
			type : 'combobox',
			javaType : 'I',
			options : {
				panelHeight : 'anto',
				data : [ {
					value : '',
					text : '全部'
				}, {
					value : '1',
					text : '男'
				}, {
					value : '2',
					text : '女'
				} ],
				onChange : function(value) {
					if (value == '') {
						$("#grid").datagrid('removeFilterRule', 'enable');
					} else {
						$("#grid").datagrid('addFilterRule', {
							field : 'sex',
							op : 'EQ',
							value : value
						});
					}
					$("#grid").datagrid('doFilter');
				}
			}
		}, {
			field : 'post',
			type : 'text',
			searchField : 'post.postName'
		}, {
			field : 'organization',
			type : 'text',
			searchField : 'organization.orgName'
		}, {
			field : 'birthday',
			type : 'datebox',
			javaType : 'D',
			op : [ 'EQ', 'NEQ', 'GT', 'LT', 'GTE', 'LTE' ]
		}, {
			field : 'graduateDate',
			type : 'datebox',
			javaType : 'D',
			op : [ 'EQ', 'NEQ', 'GT', 'LT', 'GTE', 'LTE' ]
		} ]);
	}
	var $tree;
	var $grid;
	var selectTableRow;//选中行
	var $postSelector;//选中行
	$(function() {
		$tree = $("#tree").tree({
			method : "get",
			lines : true,
			url : "${ctx}/basic/organization/tree/",
			dataPlain : true, //该属性用以启用当前 easyui-tree 控件对平滑数据格式的支持
			toggleOnClick : true,
			onSelect : function(node) {
				selectTableRow = node;
				refreshstaffinfo();
				//列表
				$postSelector = $("#post").comboselector($.extend(postSelectOpts, {
					url : "${ctx}/basic/post/dataGrid/" + selectTableRow.id + "?search_EQ_enable=1"
				}));
			}
		});

		$grid = $("#grid").datagrid($.extend(datagridOption, {
			//url:"${ctx}/basic/staffinfo/dataGrid",
			title : "人员管理 ",
			columns : [ [ {
				field : 'ck',
				checkbox : true
			}, {
				field : 'organization',
				formatter : function fmt_orgn(val, row) {
					if (val) {
						return val.orgName;
					} else {
						return '<span style="color:red">未关联</span>';
					}
				},
				title : '所属组织',
				sortable : true,
				width : 100
			}, {
				field : 'post',
				title : '主岗位',
				formatter : function fmt_orgn(val, row) {
					if (val) {
						return val.postName;
					} else {
						return '<span style="color:red">未关联</span>';
					}
				},
				sortable : true,
				width : 100
			}, {
				field : 'staffName',
				title : '员工姓名',
				sortable : true,
				width : 100
			}, {
				field : 'isPostLeader',
				title : '是否为主岗位负责人',
				sortable : true,
				formatter : function(value, row, index) {
					return '<input class="easyui-switchbutton" ' + (value == 1 ? "checked" : "")
					+ ' data-options="readonly:true,onText:\'是\',offText:\'否\'">';
				},
				width : 100
			}, {
				field : 'identityCardNo',
				title : '身份证号码',
				sortable : true,
				width : 100
			}, {
				field : 'sex',
				title : '性别',
				formatter : function(value, row, index) {
					return '<input class="easyui-switchbutton" ' + (value == 1 ? "checked" : "")
					+ ' data-options="readonly:true,onText:\'男\',offText:\'女\'">';
				},
				width : 80
			}, {
				field : 'birthday',
				title : '出生年月',
				sortable : true,
				width : 100
			}, {
				field : 'nation',
				title : '民族',
				sortable : true,
				width : 80
			}, {
				field : 'politicalLandscape',
				title : '政治面貌',
				sortable : true,
				width : 100
			}, {
				field : 'jobNumber',
				title : '工号',
				sortable : true,
				width : 100
			}, {
				field : 'workPhone',
				title : '工作电话',
				sortable : true,
				width : 100
			}, {
				field : 'phoneNumber',
				title : '电话号码',
				sortable : true,
				width : 100
			}, {
				field : 'shortNumber',
				title : '短号',
				sortable : true,
				width : 80
			}, {
				field : 'email',
				title : '电子邮箱',
				sortable : true,
				width : 100
			}, {
				field : 'homeAddress',
				title : '家庭住址',
				sortable : true,
				width : 120
			}, {
				field : 'bankAccount',
				title : '银行账号',
				sortable : true,
				width : 100
			}, {
				field : 'bankName',
				title : '开户银行',
				sortable : true,
				width : 100
			}, {
				field : 'educational',
				title : '学历',
				sortable : true,
				width : 80
			}, {
				field : 'schools',
				title : '最后毕业学校',
				sortable : true,
				width : 100
			}, {
				field : 'specialty',
				title : '专业',
				sortable : true,
				width : 100
			}, {
				field : 'graduateDate',
				title : '毕业时间',
				sortable : true,
				width : 100
			}, {
				field : 'enable',
				title : '是否启用',
				formatter : function(value, row, index) {
					return '<input class="easyui-switchbutton" ' + (value == 1 ? "checked" : "")
					+ ' data-options="readonly:true,onText:\'是\',offText:\'否\'">';
				},
				width : 80
			} ] ],
			onClickRow : function(rowIndex, rowData) {
				selectedCar = rowData;
			}
		}));

		datagridField();
		
		$("#isPostLeaderSB").switchbutton({
			onText : "是",
			offText : "否",
			checked : true,
			onChange : function(checked) {
				if (checked) {
					$("#isPostLeader").val(1);
				} else {
					$("#isPostLeader").val(2);
				}
			}
		});
		
		$("#sexSB").switchbutton({
			onText : "男",
			offText : "女",
			checked : true,
			onChange : function(checked) {
				if (checked) {
					$("#sex").val(1);
				} else {
					$("#sex").val(2);
				}
			}
		});
		
		$("#enableSB").switchbutton({
			onText : "是",
			offText : "否",
			checked : true,
			onChange : function(checked) {
				if (checked) {
					$("#enable").val(1);
				} else {
					$("#enable").val(2);
				}
			}
		});
		
		//增加按钮
		$("#add").click(function() {
			$("#fm").form("clear");
			$('#fm').form('load', {
				"organization" : selectTableRow.attributes ? selectTableRow.attributes.id : "",
				"orgName" : selectTableRow.attributes ? selectTableRow.attributes.orgName : "",
				enable : 1,
				isPostLeader : 2,
				sex : 1
			});
			$("#isPostLeaderSB").switchbutton("check");
			$("#sexSB").switchbutton("check");
			$("#enableSB").switchbutton("check");
			console.info(selectTableRow.attributes.id);
			console.info(selectTableRow.attributes.orgName);
			$("#dlg").dialog("open").dialog("setTitle", "新增人员信息");
		});

		//编辑按钮
		$("#edit").click(function() {
			var rowData = $grid.datagrid("getSelected");
			if (rowData == null) {
				$.messager.show({
					msg : "您未选中行，无法编辑",
					icon : "warning",
					position : "bottomRight"
				});
			} else {
				$("#dlg").dialog("open").dialog("setTitle", "编辑人员信息");
				$("#fm").form("load", $.extend(rowData, {
					"organization" : rowData.organization ? rowData.organization.id : "",
					"orgName" : rowData.organization ? rowData.organization.orgName : ""
				}));
				$("#isPostLeaderSB").switchbutton(rowData.enable == 1 ? "check" : "uncheck");
				$("#sexSB").switchbutton(rowData.enable == 1 ? "check" : "uncheck");
				$("#enableSB").switchbutton(rowData.enable == 1 ? "check" : "uncheck");
				rowData.post && $("#post").comboselector("setValue", rowData.post.id).comboselector("setText", rowData.post.postName);
			}
		});

		//删除按钮
		$("#deleteold").click(function() {
			var rowData = $grid.datagrid("getSelected");
			if (rowData == null) {
				$.messager.show({
					msg : "您未选中行，无法删除",
					icon : "warning",
					position : "bottomRight"
				});
			} else {
				$.messager.confirm("您确定要进行该操作？", function(c) {
					if (c) {
						$.get("${ctx}/basic/staffinfo/delete/" + rowData.id, function(result) {
							$.messager.show({
								msg : result.msg,
								icon : "info",
								position : "bottomRight"
							});
							if (result.success) {
								$grid.datagrid("load");
							}
						}, 'json');
					}
				});
			}
		});
		$("#delete").click(function() {
			multiDeleteDataGrid($grid, "${ctx}/basic/staffinfo/multiDelete");
		});

		//查询
		$("#search").click(function() {
			$grid.datagrid({
				url : "${ctx}/basic/staffinfo/dataGrid"
			});
			datagridField();
		});
		//导入
		$("#excelImport").click(function() {
			$.easyui.importor.showImportor({
				importNo : "staffinfo",
				onExportSuccess : function(file, data) {
					$grid.datagrid('load');
				}
			});
		});
		//导出
		$("#excelExport").click(function() {
			var url = "${ctx}/basic/staffinfo/excelExport?" + $.param($("#grid").datagrid('options').queryParams);
			if ($("#grid").datagrid("options").filterRules) {
				url += "&filterRules=" + JSON.stringify($("#grid").datagrid("options").filterRules);
			}
			window.parent.location.href = url;
		});

		/*分配用户*/
		$("#fpuser").click(function() {
			//fpUserDataGrid($grid, "${ctx}/basic/staffinfo/fpUser");
			var rows = $grid.datagrid('getChecked');
			var ids = "";
			if (rows) {
				$.each(rows, function(index, value) {
					ids += (value.id + ",")
				});
				if (ids == "") {
					$.messager.alert("操作提醒", "请先勾选需要分配账号的员工", "warning");
				} else {
					$.messager.confirm("您确定对勾选的" + rows.length + "个员工分配登陆账号吗？", function(c) {
						if (c) {
							$.post("${ctx}/basic/staffinfo/fpUser", {
								ids : ids
							}, function(result) {
								showInfo(result.msg);
								if (result.success) {
									$grid.datagrid("load").datagrid("clearSelections").datagrid("clearChecked");

								}
							}, 'json');
						}
					});
				}
			} else {
				$.messager.alert("操作提醒", "请先勾选需要分配账号的员工", "warning");
			}
		});

	});

	//员工信息保存
	function fn_save() {
		$("#fm").form("submit", {
			url : "${ctx}/basic/staffinfo/update",
			success : function(data) {
				$grid.datagrid("load");
				$.messager.show({
					msg : data.msg,
					icon : "info",
					position : "bottomRight"
				});
				$("#dlg").dialog('close');
			}
		});
	}

	//刷新人员管理
	function refreshstaffinfo() {
		if (!selectTableRow) {
			showAlert("请先选择人员信息");
			return false;
		}
		$grid.datagrid({
			url : "${ctx}/basic/staffinfo/dataGrid?search_EQ_organization.id=" + selectTableRow.id
		});
		datagridField();
	}
</script>

</head>
<body>
	<!-- 人员信息管理 -->

	<div data-options="region:'west',title:'组织机构',split:true" style="width: 200px;">
		<ul id="tree">
		</ul>
	</div>

	<div data-options="region:'center',border:false" style="padding: 1px;">
		<table id="grid">
		</table>
		<div id="tt" class="row">
		<div class="col-md-12">

		<button type="button" id="search" class="btn btn-primary">
					<span class="glyphicon glyphicon-search"></span> 查询所有
				</button>
				
		<shiro:hasPermission name="staffinfo:update">
				<button id="add" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-plus"></span> 新增
				</button>
				<button id="edit" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-pencil"></span> 编辑
				</button>
			</shiro:hasPermission>
			<shiro:hasPermission name="staffinfo:delete">
				<button id="delete" type="button" class="btn btn-danger">
					<span class="glyphicon glyphicon-minus"></span> 删除
				</button>
			</shiro:hasPermission>
			<shiro:hasPermission name="staffinfo:import">
				<button id="excelImport" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-open"></span> Excel导入
				</button>
			</shiro:hasPermission>
			<shiro:hasPermission name="staffinfo:export">
				<button id="excelExport" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-save"></span> Excel导出
				</button>
			</shiro:hasPermission>
			<shiro:hasPermission name="staffinfo:fpUser">
				<button id="fpuser" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-tags"></span> 分配登陆账号
				</button>
			</shiro:hasPermission>
		</div>
		</div>
	</div>

	<!-- 人员管理表单 -->
	<div id="dlg" class="easyui-dialog" style="width: 935px; height: 440px; padding: 10px 20px"
		data-options="resizable:true,modal:true, buttons:'#dlg-btn',closed:true">
		<form id="fm" method="post">
			<input type="hidden" name="id" /> <input type="hidden" name="organization">
			<table id="tbl">
				<tr>
					<td><label>所属组织:</label></td>
					<td><input id="org" name="orgName" class="easyui-validatebox" readonly="readonly"></td>
					<td><label>主岗位:</label></td>
					<td><input id="post" name="post" class="easyui-validatebox"></td>
					<td><label>员工姓名:</label></td>
					<td><input name="staffName" class="easyui-validatebox" required="required"></td>
				</tr>
				<tr>
					<td><label>是否负责人:</label></td>
					<td><input id="isPostLeader" name="isPostLeader" type="hidden"> <input id="isPostLeaderSB"></td>
					<td><label>身份证号码:</label></td>
					<td><input name="identityCardNo" class="easyui-validatebox"></td>
					<td><label>性别:</label></td>
					<td><input id="sex" name="sex" type="hidden"> <input id="sexSB"></td>
				</tr>
				<tr>
					<td><label>专业:</label></td>
					<td><input name="specialty" class="easyui-validatebox"></td>
					<td><label>出生年月:</label></td>
					<td><input name="birthday" class="easyui-datebox"></td>
					<td><label>毕业时间:</label></td>
					<td><input name="graduateDate" class="easyui-datebox"></td>
				</tr>
				<tr>
					<td><label>民族:</label></td>
					<td><input name="nation" class="easyui-validatebox"></td>
					<td><label>政治面貌:</label></td>
					<td><input name="politicalLandscape" class="easyui-validatebox"></td>
					<td><label>工号:</label></td>
					<td><input name="jobNumber" class="easyui-validatebox" required="required"></td>
				</tr>
				<tr>
					<td><label>工作电话:</label></td>
					<td><input name="workPhone" class="easyui-validatebox"></td>
					<td><label>电话号码:</label></td>
					<td><input name="phoneNumber" class="easyui-validatebox"></td>
					<td><label>短号:</label></td>
					<td><input name="shortNumber" class="easyui-validatebox"></td>
				</tr>
				<tr>
					<td><label>电子邮箱:</label></td>
					<td><input name="email" class="easyui-validatebox" data-options="validType:'email'"></td>
					<td><label>家庭住址:</label></td>
					<td><input name="homeAddress" class="easyui-validatebox"></td>
					<td><label>银行账号:</label></td>
					<td><input name="bankAccount" class="easyui-validatebox"></td>
				</tr>
				<tr>
					<td><label>开户银行:</label></td>
					<td><input name="bankName" class="easyui-validatebox"></td>
					<td><label>学历:</label></td>
					<td><input name="educational" class="easyui-validatebox"></td>
					<td><label>最后毕业学校:</label></td>
					<td><input name="schools" class="easyui-validatebox"></td>
				</tr>
				<tr>
					<td><label>是否启用:</label></td>
					<td><input id="enable" name="enable" type="hidden"> <input id="enableSB"></td>
				</tr>
			</table>
		</form>
		<div id="dlg-btn">
				<button id="add" type="button" class="btn btn-primary" onclick="fn_save();">
					<span class="glyphicon glyphicon-ok"></span> 保存
				</button>
				<button id="edit" type="button" class="btn btn-primary" onclick="$('#dlg').dialog('close');">
					<span class="glyphicon glyphicon-remove"></span> 取消
				</button>
			</div>
	</div>

</body>
</html>